<%@page import="com.seniorproject.aims.PasswordHandler"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>
<%@ page import = "java.io.FileInputStream" %>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.Date" %>
<%@ page import = "java.text.SimpleDateFormat" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Home</title>
<link rel="stylesheet" type="text/css" href="css/stylev2.css" />

	<style type="text/css">
		 a:hover {
		  	cursor:pointer;
		 }
		 a {
	      	text-decoration:none;
	   	}
	</style>

</head>
<body>

<%


	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
			
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
		System.out.println("@student_home");	
	
		//////////get permission ///////////
		
		String name = String.valueOf(session.getAttribute("name"));
		String surname = String.valueOf(session.getAttribute("surname"));
		String role_name = String.valueOf(session.getAttribute("student_role"));
		
		String department_id = String.valueOf(session.getAttribute("department_id"));

		int num_system = Integer.parseInt(String.valueOf(session.getAttribute("num_system")));

		
				
		System.out.println("name : " + name);///////////////////////////////
		System.out.println("department_id : " + department_id);/////////////////////////
		
		
		Permission permission = new Permission(strUser,name);
		permission.setDepartmentId(department_id);
		permission.setStudentCreate(Boolean.valueOf(String.valueOf(session.getAttribute("student_create"))));
		permission.setStudentDelete(Boolean.valueOf(String.valueOf(session.getAttribute("student_delete"))));
		permission.setStudentRead(Boolean.valueOf(String.valueOf(session.getAttribute("student_read"))));
		permission.setStudentUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("student_update"))));
		
		String permiss = "";
		if(permission.getStudentCreate()) {
			permiss = permiss + "Add";
		}
		if(permission.getStudentUpdate()) {
			if("".equals(permiss)) {
				permiss = permiss + "Edit";
			} else {
				permiss = permiss + " Edit";
			}
		}
		if(permission.getStudentRead()) {
			if("".equals(permiss)) {
				permiss = permiss + "Read";
			} else {
				permiss = permiss + " Read";
			}
		}
		if(permission.getStudentDelete()) {
			if("".equals(permiss)) {
				permiss = permiss + "Delete";
			} else {
				permiss = permiss + " Delete";
			}
		}
	
		//check permission
		if(!permission.getStudentRead()){%>
			<script>alert("This user dont have PERMISSION to access this zone.");</script>
			<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=student_home.jsp"> 	
		<%}
		
		//set Database Connection
		String hostProps = "";
		String usernameProps  = "";
		String passwordProps  = "";
		String databaseProps = "";
		
		try {
			//get current path
			ServletContext servletContext = request.getSession().getServletContext();
			
			InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
			Properties props = new Properties();
			
			props.load(input);

			hostProps  = props.getProperty("host");
			usernameProps  = props.getProperty("username");
			passwordProps  = props.getProperty("password");
			databaseProps = props.getProperty("database");
		} catch (Exception e) { 
			out.println(e);  
		}
		
		// connect database
		Connection connect = null;		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		
			connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
					"?user=" + usernameProps  + "&password=" + passwordProps + "&characterEncoding=tis620" );
		
			if(connect != null){
				System.out.println("Database Connect Sucesses.");
			} else {
				System.out.println("Database Connect Failed.");	
			}

		} catch (Exception e) {
			out.println(e.getMessage());
			e.printStackTrace();
		}
		
		// select picturer
		String src_path_full = null;
		try {
			// case user is lecturer
			String sql_lec = "SELECT L.picture "
							+ " FROM user U,  account A, lecturer L "
							+ " WHERE U.username=A.username AND A.index=L.index "
							+ " AND U.username='"+strUser+"'";
			
			ResultSet rs_lec = connect.createStatement().executeQuery(sql_lec);
			
			if(rs_lec.next()) {
				src_path_full = rs_lec.getString("picture");
			}
			else {
				// case user is employee
				String sql_emp = "SELECT E.picture "
						+ " FROM user U,  account A, employee E "
						+ " WHERE U.username=A.username AND A.index=E.index "
						+ " AND U.username='"+strUser+"'";
		
				ResultSet rs_emp = connect.createStatement().executeQuery(sql_emp);
				
				if(rs_emp.next()) {
					src_path_full = rs_emp.getString("picture");
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		String path_show = null;
		if((src_path_full!=null)&&(!("null").equals(src_path_full)))
		{
			String[] src_path_temp = src_path_full.split("/");	
			int length = src_path_temp.length;
			
			String src_path_a = src_path_temp[length-2];
			String src_path_b = src_path_temp[length-1];
			
			String src_path = src_path_a+"/"+src_path_b;
			
			path_show = src_path;
		
		}else{
			path_show = "upload/default_person_icon.jpg";
			
		}

		connect.close();
%>

<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser %> &nbsp;&nbsp;</font></label></p>	
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>
				</tr>
			</table>
			<table border="0">
				<tr style="font-size:10px" >
					<td width = "720" height="50" style= "background-color:#FFFFFF ; border-bottom-color: #FFFFFF; border-left-color: #FFFFFF">
						<center><h2><font color="#273b80"><label>หน้าแรก</label></font></h2></center>
					</td>
	<%
			if(permission.getStudentRead()) {
	%>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="student_search.jsp"><h2><font color="#273b80"><label style="cursor: pointer;">ค้นหา</label></font></h2></a></center>
					</td>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="student_summary.jsp" style="cursor: pointer;"><h2><font><label style="cursor: pointer;">สรุปจำนวนนักศึกษา</label></font></h2></a></center>
					</td>
	<%
			}
			if(permission.getStudentCreate()) {
	%>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="student_add.jsp" style="cursor: pointer;"><h2><font><label style="cursor: pointer;">เพิ่มนักศึกษา</label></font></h2></center>
					</td>
	<%
			}
			
			if(permission.getStudentUpdate()) {
	%>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="student_edit.jsp"><h2><font color="#273b80"><label style="cursor: pointer;">แก้ไขข้อมูล</label></font></h2></a></center>
					</td>
	<%
			}
	%>
				</tr>
			</table>
				
				<div id="main-content-ac">
						<br>
					
						<h2 style="padding-left: 2.5cm; color: #273b80; font-size: 24px;">ยินดีต้อนรับ : <%=permission.getName()%> </h2>
						
						<br><br>
						
						<center>
					
								<table id="detail_table" width="1000" border="1">
						        	<tr style="font-size:24px" >
						        		<td  width="50" height="50" rowspan="4" style="text-align: center;">
						        			<img id="user_image" src="image/user_no_photo.jpg" style="width:130px;height:160px;	">
						        		</td>
						             	<td  width="500" >
						                	<font size="5"><strong>ชื่อ</strong></font>
						                </td>
						                <td width="400" height="50">
						                	<%=name + " " + surname %>
						                </td>
						            </tr>
						                
						                
									<tr style="font-size:24px">
						                <td height="50">
						                	<font size="5"><strong>ตำแหน่ง</strong></font>
						                </td>
						                <td height="50">
						                	<%= role_name %>
						                </td>
						            </tr>
						                
									<tr style="font-size:24px">
						                <td height="50">
						                	<font size="5"><strong>สิทธิการใช้งานในระบบ</strong></font>
						                </td>
						                <td height="50">
						                	<%=permiss %>
						                </td>
						            </tr>
						                 
						            <tr style="font-size:24px">
						                <td height="50">
						<%
									if(num_system > 1){
						%>
										<center><a href="system_select.jsp"><font size="3" color="#ffffff"><input type="button" value="เปลี่ยนระบบ" class="buttonStyle" style="cursor: pointer;width:200px;"/></font></a></center>
						<%
									}
						%>						                	
						                </td>
						                <td height="50">
						                	<form id="form1" name="form1" method="post" action="change_password.jsp">
												<input type="hidden" id="mode" name="mode" value="student" /> 
												<input type="hidden" id="system" name="system" value="student" /> 
												<font size="3" color="#ffffff"><input type="submit" value="เปลี่ยนรหัสผ่าน" class="buttonStyle" style="cursor: pointer;width:200px;"/></font>
												
											</form>
						                </td>
						            </tr>     
						                
								</table>
								
  						</center>
  					
					   
								
						<br><br><br><br><br><br><br>
				
			<div style="clear: both;"></div>
			
			
				
			<div id="footer"></div>
			
		</div>		
			<div style="clear: both;"></div>		
   </div>


</body>
</html>